package com.wnxy.eleba.controller;

import com.github.pagehelper.PageInfo;
import com.wnxy.eleba.entity.Order;
import com.wnxy.eleba.service.OrderService_zhuying;
import com.wnxy.eleba.utils.Result;
import com.wnxy.eleba.vo.OrderQueryParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单控制器
 */

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController_zhuying {

    private OrderService_zhuying orderService;

    public final Logger logger = LoggerFactory.getLogger(OrderController_zhuying.class);

    @Autowired
    public void setOrderService(OrderService_zhuying orderService) {
        this.orderService = orderService;
    }

    @PostMapping("/findAllOrder")
    public Result findAllOrder(){
        List<Order> list = orderService.findAll();
        System.out.println(list);
        return Result.ok().data("list",list);

    }



    @PostMapping("/listOrderPageByCondition")
    public Result listOrderPageByCondition(@RequestBody OrderQueryParam OrderQueryParam){
        logger.warn(OrderQueryParam.toString());
        PageInfo<Order> pageInfo = orderService.listOrderPageByCondition(OrderQueryParam);
//        System.out.println(pageInfo);
        return Result.ok().data("pageInfo",pageInfo);
        //return Result.ok().data("total",pageInfo.getTotal()).data("list",pageInfo.getList()).data("pageSize",pageInfo.getPageSize());
    }

    @GetMapping("/findOrderById")
    public Result findOrderById(@RequestParam("OrderId") Integer OrderId){
//        System.out.println("Orderid "+OrderId);
        logger.warn("OrderId:" + OrderId);
        Order Order = orderService.findOrderById(OrderId);
        return Result.ok().data("Order",Order);
    }

    @PostMapping("/save")
    public Result saveOrder(@RequestBody Order Order){
        logger.warn("save Order:" + Order);
        boolean result = orderService.saveOrder(Order);
        logger.warn("save result:" + result);
        System.out.println(Order);
        return Result.ok().success(result);
    }

    @PostMapping("/update")
    public Result updateOrder(@RequestBody Order Order){
        boolean result = orderService.updateOrder(Order);
        return Result.ok().success(result);
    }

    @PostMapping("/delete")
    public Result deleteOrder(@RequestParam("ids")Integer... ids){
        boolean result = orderService.deleteOrder(ids);
        System.out.println("2222");
        return Result.ok().success(result);
    }

    @GetMapping("/del")
    public Result delOrder(@RequestParam("id")Integer id){


        boolean result = orderService.delOrder(id);
        System.out.println("2222");
        return Result.ok().success(result);
    }


    @GetMapping("/listOrderType")
    public Result listOrderType(){
        List<String> list = orderService.listOrderType();
//        System.out.println("999999999");
//        System.out.println(list);

        return Result.ok().data("list",list);
    }

}
